class Api::ModelsController < ApplicationController
  before_action :set_model, only: [:show, :update, :destroy]

  def index
    @result = {}
    @result[:total] = Model.filt(params[:filter]).size
    @result[:data] = Model.filt(params[:filter]).sort(params[:sort]).page(params[:page]).per(params[:limit])
    render json: @result
  end

  def show
    render json: @model
  end

  def create
    @model = Model.new(model_params)
    if @model.save
      render json: { success: true }
    else
      render json: { success: false }
    end
  end

  def update
    @model.update(model_params)
    render json: { success: true }
  end

  def destroy
    @model.destroy
    render json: { success: true }
  end

  private

  def set_model
    @model = Model.find(params[:id])
  end

  def model_params
    params.require(:model).permit(:name, :url, :id_property, :name_field)
  end
end